#!/bin/sh
#


# Test for presence of a "static void main" function
if test -z "$1" || test "$1" = "-l"; then
  echo "Plugin Controllers:"
  ( cd src/risoe/syslab/fhnew/controller/plugin; grep -l -r "public static void main\|'__main__'" . | sed -e "s!/!.!g" -e 's/\.java$//' -e 's/\.py$//' -e "s/^\\.\\.//" -e "s/^/ /" | sort )
  exit 1
fi


# Make sure the path is correctly encoded
NAME=$(echo $1 | sed -e "s/\./\//") 

# Check for the specification of a platform config file
PLATFORM_XML="conf/platform.xml"
if test -e "$2"; then
  PLATFORM_XML="$2"
fi

# Get the path of file
cd src/risoe/syslab/fhnew/controller/plugin/
FILE="$NAME.py"
if [ ! -f "$FILE" ]; then
    FILE="$NAME.java"
    if [ ! -f "$FILE" ]; then
        echo "No such controller. Execute the startController script without any arguments to see the available controllers"
    else
      echo "Starting Java Controller $CLASS"
      # Change controller name into valid java class name
      PACKAGE="risoe.syslab.fhnew.controller.plugin."
      cd ../../../../../..
      ./runClass.sh "$PACKAGE$NAME" "$PLATFORM_XML"
  fi
else
  echo "Starting Python Controller $NAME"
  exec python "$FILE" "$PLATFORM_XML"
fi
